package org.autumn.sharding;


import org.apache.commons.dbcp.BasicDataSource;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 数据分片
 * <p>
 * https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-jdbc/usage/sharding/
 */
public class TestDataSharding {
    public static void main(String[] args) throws SQLException {

        DataSource dataSource = getDataSource();

//        insert(dataSource);
//        String sql = "select * from t_order where order_id in (100)";
        String sql = "select * from t_order order by user_id";
        Statement statement = dataSource.getConnection().createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        int i = 0;
        while (resultSet.next()) {
            System.out.println(resultSet.getObject(1));
            i++;
        }
//        System.out.println(i);

    }

    private static void insert(DataSource dataSource) throws SQLException {
        Connection connection = dataSource.getConnection();
        String sql = "insert into t_order(price,user_id,status) values (100,'mikael','success')";
//        for (int i = 0; i < 10; i++) {
            Statement statement = connection.createStatement();
            statement.execute(sql);
//        }
    }

    private static DataSource getDataSource() throws SQLException {
        // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();

        // 配置第一个数据源
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://121.5.167.184:3306/sharding-jdbc?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("immortal");
        dataSourceMap.put("ds", dataSource);

//        // 配置第二个数据源
//        BasicDataSource dataSource2 = new BasicDataSource();
//        dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
//        dataSource2.setUrl("jdbc:mysql://localhost:3306/ds1");
//        dataSource2.setUsername("root");
//        dataSource2.setPassword("");
//        dataSourceMap.put("ds1", dataSource2);

        // 配置Order表规则
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order", "ds.order_${1..2}");
//        orderTableRuleConfig.setLogicTable("order");
//        orderTableRuleConfig.setActualDataNodes("ds${0..1}.t_order${0..1}");

        orderTableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("snowflake", "order_id"));

        // 配置分库 + 分表策略
//        orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
        orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "order_${order_id % 2 + 1}"));

        // 配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        // 省略配置order_item表规则...
        // ...

        Properties properties = new Properties();
        properties.put("sql.show", true);
        // 获取数据源对象
        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, properties);
    }
}
